package com.sphd.beautysalon.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sphd.beautysalon.R;
import com.sphd.beautysalon.adapters.OrderParticalarsAdapter;
import com.sphd.beautysalon.base.BaseActivity;
import com.sphd.beautysalon.base.RestaurantUrl;
import com.sphd.beautysalon.info.FoodInfo;
import com.sphd.beautysalon.info.SalesInfo;
import com.sphd.beautysalon.presenter.OrderParticaularsPresenterImpl;
import com.sphd.beautysalon.utils.Constant;
import com.sphd.beautysalon.view.OrderParticaularsView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/*
* 销售详情
* */
public class OrderParticularsActivity extends BaseActivity implements OrderParticaularsView {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    TextView orderNum;
    TextView orderTime;
    TextView orderName;
    TextView orderPhone;
    Bundle bundle;
    Intent intent;
    SalesInfo salesInfo;
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;


    TextView sphjTextview;
    TextView yhqzkTextview;
    TextView yfjeTextview;
    TextView cardPayTextview;
    TextView xjzfTextview;
    TextView cardmoneyTextview;

    TextView addressTextview;
    TextView usernameTextview;
    TextView userPhoneTextview;
    LinearLayout orderAddressLinear;
    private View heardView, footView;
    private OrderParticalarsAdapter orderParticalarsAdapter;
    private OrderParticaularsPresenterImpl orderParticaularsPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_particulars);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
        mySwf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mySwf.setRefreshing(false);
            }
        });
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        salesInfo = (SalesInfo) bundle.getSerializable(Constant.SALESINFO);
        orderParticaularsPresenter = new OrderParticaularsPresenterImpl(this);
        setTitleBar(toolbar, "销售详情");
        setRefresh(mySwf);
        setRecy(myRecy);
        initHeardView();
        initFootWiew();
        initAdapter();
        addData();
    }

    private void initFootWiew() {
        footView = View.inflate(context, R.layout.order_particulars_foot, null);
        sphjTextview = $(R.id.sphj_textview, footView);
        yhqzkTextview = $(R.id.yhqzk_textview, footView);
        yfjeTextview = $(R.id.yfje_textview, footView);

        cardPayTextview = $(R.id.card_pay_textview, footView);
        xjzfTextview = $(R.id.xjzf_textview, footView);

        cardmoneyTextview = $(R.id.cardmoney_textview, footView);

        orderAddressLinear = $(R.id.order_address_linear, footView);

        addressTextview = $(R.id.address_textview, footView);
        usernameTextview = $(R.id.username_textview, footView);
        userPhoneTextview = $(R.id.user_phone_textview, footView);

        sphjTextview.setText("￥" + (turn(salesInfo.getOrder_ticket() +
                salesInfo.getOrder_lastprice() + salesInfo.getOrder_cardpay())));
        yhqzkTextview.setText("￥" + turn(salesInfo.getOrder_ticket()));
        yfjeTextview.setText("￥" + (turn(salesInfo.getOrder_lastprice() + salesInfo.getOrder_cardpay())));
        cardPayTextview.setText("￥" + turn(salesInfo.getOrder_cardpay()));
        xjzfTextview.setText("￥" + turn(salesInfo.getOrder_lastprice()));
        addressTextview.setText("" + salesInfo.getOrder_addr());
        usernameTextview.setText("" + salesInfo.getOrder_recName());
        userPhoneTextview.setText("" + salesInfo.getOrder_uphone());
        cardmoneyTextview.setText(turn(salesInfo.getMem_account()));
        if (salesInfo.getOrder_isosale() == 1) {
            orderAddressLinear.setVisibility(View.VISIBLE);
        } else {
            orderAddressLinear.setVisibility(View.GONE);
        }
    }

    private void addData() {
        orderParticaularsPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.ORDER_GET_FOOD + "orderno=" + salesInfo.getOrder_no());
    }

    private void initHeardView() {
        heardView = View.inflate(context, R.layout.order_particulars_heard, null);
        orderNum = $(R.id.order_num, heardView);
        orderTime = $(R.id.order_time, heardView);
        orderName = $(R.id.order_name, heardView);
        orderPhone = $(R.id.order_phone, heardView);
        orderName.setText(salesInfo.getOrder_username());
        orderNum.setText(salesInfo.getOrder_no());
        orderPhone.setText(salesInfo.getOrder_memphone());
        orderTime.setText(salesInfo.getOrder_fintime());
    }

    private void initAdapter() {
        orderParticalarsAdapter = new OrderParticalarsAdapter(R.layout.item_order_particulars, new ArrayList<>(), salesInfo, this);
        myRecy.setAdapter(orderParticalarsAdapter);
        orderParticalarsAdapter.addHeaderView(heardView);
        orderParticalarsAdapter.addFooterView(footView);

    }

    @Override
    public void recRes(List<FoodInfo> foodInfoList) {
        orderParticalarsAdapter.setNewData(foodInfoList);
    }


    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
